package madixin.t594;

import java.util.HashMap;
import java.util.Map;

class Solution {
    public int findLHS(int[] nums) {

        Map<Integer, Integer> index = new HashMap<>();
        for (int n : nums) {
            if (!index.containsKey(n)) {
                index.put(n, 1);
            } else {
                index.put(n, index.get(n) + 1);
            }
        }

        int res = 0;
        for (int n : index.keySet()) {
            if (index.containsKey(n + 1) && index.get(n) + index.get(n + 1) > res) {
                res = index.get(n) + index.get(n + 1);
            }
        }

        return res;

    }
}